<?php  
$pPageIsPublic = false;
include '../_common.php';
	
$_SESSION["m1"]	= "content";
$_SESSION["m2"] = 'building-list';

$pTitle  = 'New Building';
$pButton = 'Save';
$action  = 'add';
$id 	 = intval($_REQUEST['id']);
$seo	 = NULL;


$objBuild = new Building();
$objBuild ->setUid($id);
if ($objBuild ->load()) {
	$pTitle  = 'Edit Building';
	$pButton = 'Update';
	$action  = 'edit';
	//$seo = json_decode($objBuild ->seo);
	
	if ($objBuild ->parentId == PARENT_PAGE) Tzn::redirect("building-list.php", "Error: invalid argument.", 1);  
	
}



if (isset($_POST['submit'])) {
	if (count($_POST['categoryId'])==0) {
		print 'select one or more category Neighborhoods';
	} else {
		$objBuild ->setAuto($_POST);
		//$objBuild ->seo = arr2json($_POST, array('metaTitle', 'metaDescription', 'metaKeyword', 'friendlyUrl'));
		if ($_POST['action'] == 'edit') {
			$objBuild ->getConnection();
			$objBuild ->setUid($id);
			$objBuild ->update();
			$message = "Building has been updated.";

		} elseif ($_POST['action'] == 'add') {
			$objBuild ->add();
			$message = "Building has been added.";
		}
		
		$catpage = new CategoryPage();
		$catpage ->getConnection();
		$catpage ->query("DELETE FROM {$catpage ->gTable()} WHERE pageId='{$objBuild ->id}'");
		
		foreach ($_POST['categoryId'] as $cId)
			$catpage ->query("INSERT INTO {$catpage ->gTable()} (pageId, categoryId) VALUES ('{$objBuild ->id}', '{$cId}')");
		
		Tzn::redirect('building-add.php?id='. $objBuild ->id, $message);
	
	}
}

?>
<?php include("header.php"); ?>
<?php
$tabs 	= array('images' => 1, 'video' => 2, 'map' => 3);
$selTab = $tabs[$_REQUEST['tab']] ? $tabs[$_REQUEST['tab']] : 0;
?>
<script type="text/javascript">
$(document).ready(function(){
	$('#frmMain').validate();
	$('#content-tabs').tabs({
		disabled :[<?php echo !$objBuild ->isLoaded() ? "2,3,4" : "" ?>],
		initial  : <?php echo $selTab ?>
	});

	$('#categoryId').multiSelect({ selectAll: false, oneOrMoreSelected: '*' });
		
});
</script>
<?php include("sidebar.php"); ?>
<ul id="subnav" >
	<li ><a class="back" href="building-list.php" > Back </a></li>
	<li class="noborder" >
		<a class="add" href="building-add.php" > Add a New Building </a>
	</li>
</ul>
<div id="list">
	<h2> <?php echo $pTitle;?> </h2>
    <div id="content-tabs">
		<ul>
			<li><a href="#fragment-1" onclick="location.href='<?= $_SERVER['PHP_SELF']."?id={$objBuild ->id}" ?>';" ><span>Building Details</span></a></li>
			<li><a href="#fragment-2"><span>Photo Gallery</span></a></li>
			<li><a href="#fragment-3"><span>Video Tour</span></a></li>
			<li><a href="#fragment-4" onclick="location.href='<?= $_SERVER['PHP_SELF']."?id={$objBuild ->id}&tab=map" ?>';"  ><span>View Map</span></a></li>
		</ul>
		<div id="fragment-1" >
			<form action="" method="post" name = "frmMain" id = "frmMain"  >
			<fieldset><legend> General Info </legend>
					<table cellpadding="0" cellspacing="0" class="table-form"  >
						<tr>
							<th width="12%"  >Neighborhood: </th>
							<td width="88%" >
							<?php $objBuild ->pSelectCheckBoxCategories(); ?>
							</td>
						</tr>
						<tr>
							<th>Title: </th>
							<td><?php $objBuild ->qText('title', '', 'width:548px', 'class="required"');?></td>
						</tr>
						<tr>
							<th>Description: </th>
							<td><?php $objBuild ->qTextArea('description', '', 'width:548px; height:80px;'); ?></td>
						</tr>
						<tr>
							<th>Address: </th>
							<td><?php $objBuild ->qText('address', '', 'width:548px;'); ?></td>
						</tr>
						<tr>
							<th>Location: </th>
							<td><?php $objBuild ->qText('location', '', 'width:548px;'); ?></td>
						</tr>
						<tr>
							<th>Total Units: </th>
							<td><?php $objBuild ->qText('total_units', '', 'width:100px;'); ?></td>
						</tr>
						<tr>
							<th>Total Stories: </th>
							<td><?php $objBuild ->qText('total_stories', '', 'width:100px;'); ?></td>
						</tr>
						<tr>
							<th>Bedrooms: </th>
							<td><?php $objBuild ->qText('bedrooms', '', 'width:548px;'); ?></td>
						</tr>
						<tr>
							<th>Unit Sizes: </th>
							<td><?php $objBuild ->qText('unit_sizes', '', 'width:548px;'); ?></td>
						</tr>
						<tr>
							<th>Parking: </th>
							<td><?php $objBuild ->qText('parcking', '', 'width:548px;'); ?></td>
						</tr>
						<tr>
							<th>Security: </th>
							<td><?php $objBuild ->qTextArea('security', '', 'width:548px; height:80px;'); ?></td>
						</tr>
						<tr>
							<th>Year Built: </th>
							<td><?php $objBuild ->qText('year_built', '', 'width:100px;'); ?></td>
						</tr>
						<tr>
							<th>Amenities: </th>
							<td><?php $objBuild ->qTextArea('amenities', '', 'width:548px; height:80px;'); ?></td>
						</tr>
						<tr>
							<th>Status: </th>
							<td><?php $objBuild ->arrayToSelect($objBuild ->_arrStatus, 'status', $objBuild ->status, false, 'width:114px', '');?></td>
						</tr>
					</table>
			</fieldset>
			<div style="margin-top:10px">
				<input type="hidden" name="id" value="<?php echo $id; ?>" />
				<input type="hidden" name="action" value="<?php echo $action; ?>" />
                <input type="submit" name="submit"  value="<?php echo $pButton; ?>" /> &nbsp; <input type="reset" value="Cancel" onClick="window.location.href='building-list.php'" />
            </div>
            </form>
		</div>    
		<div id="fragment-2" >
			<?php
			$objPage = clone $objBuild;   
			include PRJ_INCLUDE_PATH.'admin/page-image-list.php'; ?>
		</div>
		<div id="fragment-3" >
<?php
	if ($objBuild ->isLoaded()){
?>
<script type="text/javascript">
$().ready(function(){
	$('ul.videos').admin_sortable("File");
	$('#selectedAll').checkedToggle("ul.videos", "File");
});
</script>
			<fieldset style="padding-top: 0;" ><legend> Video Tour List </legend>
				<form action="<?php echo $_SERVER["PHP_SELF"]."?id=". $objPage->id. "&tab=video" ?>" method="post" name="frmFilter" >
					<ul id="subnav" >
						<li class="noleft" ><input type="checkbox" name="selectedAll" id="selectedAll" /><label for="selectedAll" class="first-check" >Select All</label></li>
						<li><a href="javascript:void(0);" id="remAll">Delete Selecctioned</a></li>
			   			<li><a class="add" href="<?="video-add.php?bid={$objBuild ->id}"?>" > Add a New Video Tour </a></li>
			   			<li class="noborder"  ><?php include("filter.php"); ?></li>
					</ul>
			 	</form>
			 	<div id="list-boxes"  >
            		<ul class="videos">
<?php

	$objFile = new File();
    if ($objFile ->loadItems(null, "#status like '". $chkstatus ."%' AND LENGTH(#video)>0 AND #pageId=". $objPage ->id)) {
    	while ($objItem = $objFile->rNext()) {    
                ?>                   
						<li id="recordsArray_<?php echo $objItem->id; ?>" class="<?php echo $objItem->status=='Draft' ? 'draft' : ''; ?>" >
							<div class="pic-drag" >
								<?php echo $objItem->getImage(); ?>
								<label for="file-<?php echo $objItem->id ?>" ><input class="checks"   name="items[]" id="file-<?php echo $objItem->id;?>" type="checkbox" value="<?php echo $objItem->id;?>" />
												 <?php echo truncate_string($objItem->title, 12);?></label>
                                <p class="itemsubnav" >
                                	<?php if(valid_youtube_id($objItem ->video)){ ?><a rel="shadowbox[videos];width=405;height=340;player=swf" title="<?php echo $objItem->title ?>" href="http://www.youtube.com/v/<?= $objItem ->video ?>&hl=en&fs=1&rel=0&autoplay=1"  >Preview</a><?php } 
                                	else {print '<label class="error"> Youtube Video ID Inavlid. </label>';} ?>
                                    <a href="video-add.php?id=<?php echo $objItem->id ?>&bid=<?php echo $objItem ->page ->id ?>" title="Edit" >Edit</a>
                                    <a id="del" href="javascript:void(0);" title="" >Remove</a>
                                </p>
                            </div>
                        </li>	
<?php 	}	unset($objItem);
    } ?>                    					
            		</ul>            		
            		<div class="clear"></div>
  			</div>
			</fieldset>
<?php 
	}
?>
		</div>
		<div id="fragment-4" >
<?php
	if ($objBuild ->isLoaded()){
		print '<script>
		$(window).load(function () {
			$(\''.
				'<iframe src="viewmap.php?id='. $objBuild ->id .'&lat='. (!$objBuild ->lat ? TZN_GEO_LAT : $objBuild ->lat ) .'&lng='. (!$objBuild ->lng ? TZN_GEO_LNG : $objBuild ->lng ) .'"  style="width:680px;height:450px;"  frameborder="0"></iframe>'	
			.'\').appendTo("#fragment-4");
			}); 
		</script>';
		
	}
?>
		
		</div>
    </div>
</div>
<?php include("footer.php"); ?>